#!/bin/bash
if [ "$1" = "" ]
then
    echo "Preparing devcontainer - installing dependencies"
    sudo apt update
    # install dependencies with bundled script
    bash prepare-software
    # install additional dependencies needed for build (not included in devcontainer base image)
    sudo apt-get -y install file cpio

    echo "Preparing devcontainer - adjusting permissions"
    # everything should be owned by container vscode user
    sudo chown -R vscode:vscode /workspaces/

    echo "Preparing devcontainer - getting buildroot"
    # get buildroot with bundled script
    bash get-buildroot

elif [ "$1" = "--remove-problematic-symlinks" ]; then
    # Symblinks might have been malformated when cloning thte repository - remove all
    SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

    rm -v $SCRIPT_DIR/../buildroot/package/spotifyd/spotifyd.mk

    rm -v $SCRIPT_DIR/../buildroot/board/raspberrypi0w \
        $SCRIPT_DIR/../buildroot/board/raspberrypi2 \
        $SCRIPT_DIR/../buildroot/board/raspberrypi3 \
        $SCRIPT_DIR/../buildroot/board/raspberrypi4

elif [ "$1" = "--fix-symlinks" ]; then
    # Symblinks might have been malformated when cloning thte repository - recreate all (experimental)
    SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

    bash $SCRIPT_DIR/prepare-devcontainer --remove-problematic-symlinks

    ln -sv spotifyd-bin-mk $SCRIPT_DIR/../buildroot/package/spotifyd/spotifyd.mk

    ln -sv raspberrypi $SCRIPT_DIR/../buildroot/board/raspberrypi0w
    ln -sv raspberrypi $SCRIPT_DIR/../buildroot/board/raspberrypi2
    ln -sv raspberrypi $SCRIPT_DIR/../buildroot/board/raspberrypi3
    ln -sv raspberrypi $SCRIPT_DIR/../buildroot/board/raspberrypi4
fi